<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户注册</title>
</head>

<!--        引入样式文件-->
<link rel="stylesheet" href="../plugins/elementui/index.css">
<link rel="stylesheet" href="../css/register.css">

<body>
<div id="register" class="box">
    <hr>
    <div class="head cover"><h3>用户注册</h3></div>
    <hr>
    <el-form :model="registerform" status-icon :rules="rules" ref="registerform" label-width="100px" class="demo-ruleForm form">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="registerform.username"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
                <el-input v-model="registerform.phone"></el-input>
            </el-form-item>
            <el-form-item label="验证码" prop="code">
                <el-input v-model="registerform.code"></el-input>
            </el-form-item>
        <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="registerform.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="registerform.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
            <el-input v-model.number="registerform.age"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('registerform')">注册</el-button>
            <el-button @click="resetForm('registerform')">重置</el-button>
        </el-form-item>
    </el-form>
</div>
</body>

<!--         引入组件库-->
<script src="../js/axios-0.18.0.js"></script>
<script src="../js/jquery.min.js"></script>
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>


<script>
    new Vue({
        el:'#register',
        data() {
            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('年龄不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 16) {
                            callback(new Error('必须年满16岁'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            var checkPhone = (rule, value, callback) => {
                var phoneReg = /(^1\d{10}$)|(^[0-9]\d{7}$)/;
                if (!phoneReg.test(value)){
                    callback(new Error('请输入正确的手机号'));
                }else {
                    callback();
                }


            }
            return {
                registerform: {
                    username:'',
                    phone:'',
                    code:'',
                    pass: '',
                    checkPass: '',
                    age: ''
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    age: [
                        { validator: checkAge, trigger: 'blur' }
                    ],
                    phone:[
                        { validator: checkPhone,trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    })
</script>
</html>